local _cur_start_pos, _cur_end_pos = 1, 7
local _inv_start_pos, _inv_end_pos = 101, 116
local _trump_start_pos, _trump_end_pos = 201, 204

-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
local function get_config(itemid)
    local configs = IMPORT_D:query("item")
    if not configs then
        return
    end

    local config = configs[itemid]
    if not config then
        return
    end

    return config
end

local function check_inventory(itemid)
    local config = get_config(itemid)
    if not config then
        return false
    end

    if config.type ~= "weapon" and
       config.type ~= "wing" and
       config.type ~= "box" then
        return false
    end

    return true
end

local function check_trump(itemid)
    local config = get_config(itemid)
    if not config then
        return false
    end

    if config.type ~= "trump" then
        return false
    end

    return true
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
INVENTORY_D = {}

----------------------
----! CURRENCY
--- 货币的处理，货币类型(type)即为存储的物品位置(pos)
----------------------

---! 增加指定货币
function INVENTORY_D:add_currency(player, id, count)
    if id < _cur_start_pos or id > _cur_end_pos then
        return
    end

    if not count then
        return
    end

    local prop = player:query_temp("item", id)
    if not prop then
        return player:set_temp("item", id, ITEM_D:clone_item({ propId = id, propCount = count, }))
    end

    prop.propCount = prop.propCount + count
    return prop
end

---! 扣除指定货币
function INVENTORY_D:cost_currency(player, id, count)
    if id < _cur_start_pos or id > _cur_end_pos then
        return
    end

    if not count then
        return
    end

    local prop = player:query_temp("item", id)
    if not prop then
        return player:set_temp("item", id, ITEM_D:clone_item({ propId = id, propCount = -count, }))
    end

    prop.propCount = prop.propCount - count
    return prop
end

---! 修改指定货币
function INVENTORY_D:change_currency(player, id, count)
    if id < _cur_start_pos or id > _cur_end_pos then
        return
    end

    if not count then
        return
    end

    local prop = player:query_temp("item", id)
    if not prop then
        return player:set_temp("item", id, ITEM_D:clone_item({ propId = id, propCount = count, }))
    end

    prop.propCount = count
    return prop
end

---! 获取指定货币数量
function INVENTORY_D:get_currency(player, id)
    if id < _cur_start_pos or id > _cur_end_pos then
        return 0
    end

    local prop = player:query_temp("item", id)
    if not prop then
        return 0
    end

    return prop.propCount or 0
end

---! 获取所有货币
function INVENTORY_D:get_all_currency(player)
    local props = {}
    for pos = _cur_start_pos, _cur_end_pos do repeat
        local item = player:query_temp("item", pos)
        if not item then
            break
        end

        table.insert(props, {
            propId = item.propId,
            propCount = INVENTORY_D:get_currency_show_count(player, pos, item.propCount or 0),
        })
    until true end
    return props
end

---! 获取货币显示数值
function INVENTORY_D:get_currency_show_count(player, id, count)
    count = tostring(count or player:get_currency(id))
    if id == GamePropIds.kGamePropIdsCopperIcon then
        return tonumber(string.sub(count, 1, -1 - (3 * (player:get_rank() - 1))))
    else
        return tonumber(count)
    end
end

----------------------
----! WEAPON AND BOX
----------------------

---! 获取空闲位置
function INVENTORY_D:get_inv_idle_pos(player)
    for pos = _inv_start_pos, _inv_end_pos do
        local weapon = player:query_temp("item", pos)
        if not weapon then
            return pos
        end
    end
end

---! 增加指定道具
function INVENTORY_D:add_prop(player, propId, reverse)
    if not check_inventory(propId) then
        return
    end

    if not reverse then
        for pos = _inv_start_pos, _inv_end_pos do repeat
            local item = player:query_temp("item", pos)
            if item then
                break
            end

            local prop = ITEM_D:clone_item({ propId = propId, pops = pos - _inv_start_pos + 1, propCount = 1, })
            return player:set_temp("item", pos, prop)
        until true end
    else
        for pos = _inv_end_pos, _inv_start_pos, -1 do repeat
            local item = player:query_temp("item", pos)
            if item then
                break
            end

            local prop = ITEM_D:clone_item({ propId = propId, pops = pos - _inv_start_pos + 1, propCount = 1, })
            return player:set_temp("item", pos, prop)
        until true end
    end
end

---! 增加指定道具
function INVENTORY_D:add_prop_by_pos(player, pops, prop)
    local pos = _inv_start_pos + pops - 1
    if pos < _inv_start_pos or pos > _inv_end_pos then
        return
    end

    return player:set_temp("item", pos, prop)
end

---! 获取指定道具
function INVENTORY_D:get_prop_by_pos(player, pops)
    local pos = _inv_start_pos + pops - 1
    if pos < _inv_start_pos or pos > _inv_end_pos then
        return
    end

    return player:query_temp("item", pos)
end

---! 移除指定道具
function INVENTORY_D:remove_prop_by_pos(player, pops)
    local pos = _inv_start_pos + pops - 1
    if pos < _inv_start_pos or pos > _inv_end_pos then
        return
    end

    local prop = player:query_temp("item", pos)
    if not prop then
        return
    end

    player:delete_temp("item", pos)
    return prop
end

---! 获取所有道具
function INVENTORY_D:query_inventory(player)
    local props = {}
    for pos = _inv_start_pos, _inv_end_pos do
        local prop = player:query_temp("item", pos)
        if prop then
            table.insert(props, {
                propId = prop.propId,
                propCount = tonumber(tostring(prop.propCount)),
                pops = prop.pops,
                level = prop.level,
            })
        end
    end
    return props
end

---! 获取最高等级的武器对象
function INVENTORY_D:get_best_weapon(player)
    local item
    for pos = _inv_start_pos, _inv_end_pos do repeat
        local weapon = player:query_temp("item", pos)
        if not weapon then
            break
        end

        if weapon.type ~= "weapon" then
            break
        end

        if item and item.lev >= weapon.lev then
            break
        end

        item = weapon
    until true end
    return item
end

---! 查找允许购买的武器对象
function INVENTORY_D:find_weapon_by_purchase(player)
    local min_level, max_level = 0, 0
    for pos = _inv_start_pos, _inv_end_pos do repeat
        local weapon = player:query_temp("item", pos)
        if not weapon then
            break
        end

        if weapon.type ~= "weapon" then
            break
        end

        local level = weapon.lev

        if min_level == 0 or min_level > level then
            min_level = level
        end

        if max_level == 0 or max_level < level then
            max_level = level
        end
    until true end

    ---! 武器框中的最低武器等级小于当前最高武器等级减5级，购买的武器为武器框中等级最低的武器
    return ITEM_D:find_weapon_by_level(math.max(1, math.min(min_level, max_level - 5)))
end

---! 提升武器等级
function INVENTORY_D:improve_weapon_level(player, weapon, num)
    ---! 改变装备ID
    weapon.propId = weapon.propId + (num or 1)

    if weapon.lev > player:get_level() then
        --!更新玩家等级
        player:set_level(weapon.lev)

        --! 更新玩家阶级
        player:set_rank(weapon.rank)

        --!奖励钻石
        player:add_currency(GamePropIds.kGamePropIdsDiamondsIcon, weapon.firstAward)
        ITEM_D:send_currency_info(player)

        ---! 同步关卡排名
        RANK_D:sync_rank_data(player)
    end
end

----------------------
----! TRUMP
----------------------

---! 获取空闲位置
function INVENTORY_D:get_trump_idle_pos(player)
    for pos = _trump_start_pos, _trump_end_pos do
        local trump = player:query_temp("item", pos)
        if not trump then
            return pos
        end
    end
end

---! 增加道具
function INVENTORY_D:add_trump(player, trumpid)
    if not check_trump(trumpid) then
        return
    end

    local pos = INVENTORY_D:get_trump_idle_pos(player)
    if not pos then
        return
    end

    local trump = {}
    trump.propId =  trumpid
    trump.propCount = 1
    trump.pops = pos - _trump_start_pos + 1
    player:set_temp("item", pos, ITEM_D:clone_item(trump))
    return trump
end

---! 消耗道具
function INVENTORY_D:use_trump(player, pops)
    local pos = _trump_start_pos + pops - 1
    if pos < _trump_start_pos or pos > _trump_end_pos then
        return
    end

    local trump = player:query_temp("item", pos)
    if not trump then
        return
    end

    player:delete_temp("item", pos)
    return trump.propId
end

---! 所有道具
function INVENTORY_D:query_trump(player)
    local trumps = {}
    for pos = _trump_start_pos, _trump_end_pos do repeat
        local trump = player:query_temp("item", pos)
        if not trump then
            break
        end

        table.insert(trumps, {
            trumpid = trump.propId,
            pops = trump.pops,
        })
    until true end
    return trumps
end
